package com.example.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.Bean.Result;
import com.example.demo.Bean.Student;
import com.example.demo.Bean.Teacher;
import com.example.demo.Mapper.StudentMapper;
import com.example.demo.Mapper.TeacherMapper;
import com.example.demo.Utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginService {


    @Value("${jwt.tokenHead}")
    private String JWT_TOKENHEAD;

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    TeacherMapper teacherMapper;


    @Autowired
    JwtTokenUtil jwtTokenUtil;


    public Result loginOfStudent(String studentUid, String password) throws Exception{
        Result result = null;
        Student student = getStudentInfo(studentUid);
        if(student==null||!password.equals(student.getPassword())){
            result = new Result(403,"用户名或密码错误！");
        }else{
            Map map = new HashMap();
            String token = jwtTokenUtil.createToken(studentUid);
            map.put("token",token);
            map.put("tokenHead",JWT_TOKENHEAD);
            result = new Result(200,map);
            student.setPassword(null);
            result.setUser(student);
        }
        return result;
    }

    private Student getStudentInfo(String studentUid) throws Exception{
        Student student = studentMapper.selectOne(new QueryWrapper<Student>().eq("student_uid",studentUid));
        return student;
    }

    public Result loginOfTeacher(String teacherUid, String password) throws Exception{
        Result result = null;
        Teacher teacher = getTeacherInfo(teacherUid);
        if(teacher==null||!password.equals(teacher.getPassword())){
            result = new Result(403,"用户名或密码错误！");
        }else{
            Map map = new HashMap();
            String token = jwtTokenUtil.createToken(teacherUid);
            map.put("token",token);
            map.put("tokenHead",JWT_TOKENHEAD);
            result = new Result(200,map);
            teacher.setPassword(null);
            result.setUser(teacher);
        }
        return result;
    }

    private Teacher getTeacherInfo(String teacherUid) throws Exception{
        Teacher teacher = teacherMapper.selectOne(new QueryWrapper<Teacher>().eq("teacher_uid", teacherUid));
        return teacher;
    }

    public Result modifyPwOfStudent(String studentUid, String newPw, String original) throws Exception{
        Student student = studentMapper.selectById(studentUid);
        if(!student.getPassword().equals(original)){
            return new Result(200, "原始密码错误！");
        }
        studentMapper.update(null,new UpdateWrapper<Student>().eq("student_uid", studentUid).set("password", newPw));
        return new Result(200,"修改成功！");
    }

    public Result modifyPwOfTeacher(String teacherUid, String newPw, String original) throws Exception{
        Teacher teacher = teacherMapper.selectById(teacherUid);
        if(!teacher.getPassword().equals(original)){
            return new Result(200, "原始密码错误！");
        }
        teacherMapper.update(null,new UpdateWrapper<Teacher>().eq("teacher_uid", teacherUid).set("password", newPw));
        return new Result(200,"修改成功！");
    }

    public Result logout(){
        return new Result(200,"退出登录");
    }

}
